package com.like.likeutils.util;

import org.xutils.x;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment extends Fragment {

	protected View mRootView;
	private boolean injected = false;

	protected boolean mPrepared = false;
	protected boolean mHasLoadOnce = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mRootView == null) {
			injected = true;
			mRootView = x.view().inject(this, inflater, container);
			onOnlyOnceInit();
			if (getUserVisibleHint())
				lazyLoad();
			mPrepared = true;
		}
		return mRootView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		if (!injected) {
			x.view().inject(this, this.getView());
		}
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (isVisibleToUser) {
			if (mPrepared) {
				onVisiable();
				lazyLoad();
			}
		} else {
			if (mPrepared)
				onInvisiable();
		}
	}

	private void lazyLoad() {
		lazyLoadEveryTime();
		if (!mHasLoadOnce) {
			lazyLoadOnce();
			mHasLoadOnce = true;
		}
	}

	protected void onOnlyOnceInit() {
	}

	protected void lazyLoadOnce() {
	}

	protected void lazyLoadEveryTime() {
	}

	protected void onVisiable() {
	}

	protected void onInvisiable() {
	}

}
